package com.zzyl.nursing.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.config.WeChatAppIdConfig;
import com.zzyl.nursing.service.WechatService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechatService {

    // 注入微信开发者的相关信息
    @Autowired
    private WeChatAppIdConfig weChatAppIdConfig;


    // 腾讯的 3个请求方式
    // 1. 获取openId的接口
    private static final String OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    //  2， 获取手机号的接口
    private static final String PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    //  3，获取 token 的接口
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";


    // 获取openid

    /**
     * @param code 临时登录的code
     * @return 返回openid
     */
    @Override
    public String getOpenid(String code) {

        System.out.println(" weChatAppIdConfig.getAppid() = " +  weChatAppIdConfig.getAppid());
        System.out.println("  weChatAppIdConfig.getAppsecret() = " +   weChatAppIdConfig.getAppsecret());

        Map<String, Object> params = new HashMap<>();
        params.put("appid", weChatAppIdConfig.getAppid());
        params.put("secret", weChatAppIdConfig.getAppsecret());
        params.put("js_code", code);
        params.put("grant_type", "authorization_code");

        HttpResponse response = HttpUtil.createRequest(Method.GET, OPENID_URL).form(params).execute();

        if (response.isOk()) {
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            String openid = jsonObject.get("openid", String.class);
            System.out.println("openid = " + openid);
            System.out.println("openid = " + openid);
            return openid;

        }
        return null;
    }

    // 获取用户手机号

    /**
     * @param phoneCode // 前端传来的用户加密的手机号code
     * @return
     */
    @Override
    public String getPhone(String phoneCode) {
        System.out.println(" weChatAppIdConfig.getAppid() = 2" +  weChatAppIdConfig.getAppid());
        System.out.println("  weChatAppIdConfig.getAppsecret() =2 " +   weChatAppIdConfig.getAppsecret());

        String token = getToken();

        Map<String, Object> params = new HashMap<>();
        params.put("code", phoneCode);
        String jsonStr = JSONUtil.toJsonStr(params);
        HttpResponse response = HttpUtil.createRequest(Method.POST, PHONE_URL + token).body(jsonStr).execute();

        if (response.isOk()) {
            // 获取请求数据
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            String phoneNumber = jsonObject.getByPath("phone_info.phoneNumber", String.class);
            System.out.println("phoneNumber = " + phoneNumber);
            return phoneNumber;
        }

        return null;
    }

    //     获取token
    public String getToken() {
        System.out.println(" weChatAppIdConfig.getAppid() =3 " +  weChatAppIdConfig.getAppid());
        System.out.println("  weChatAppIdConfig.getAppsecret() =3 " +   weChatAppIdConfig.getAppsecret());

        Map<String, Object> params = new HashMap<>();
        params.put("grant_type", "client_credential");
        params.put("appid", weChatAppIdConfig.getAppid());
        params.put("secret", weChatAppIdConfig.getAppsecret());
        // String jsonStr = JSONUtil.toJsonStr(params);

        HttpResponse response = HttpUtil.createRequest(Method.GET, TOKEN_URL).form(params).execute();

        if (response.isOk()) {
            String body = response.body();
            JSONObject jsonObject = JSONUtil.parseObj(body);
            String token = jsonObject.get("access_token", String.class);
            System.out.println("token = " + token);
            return token;

        }


        return null;
    }
}
